package com.example.linktop.newlistviewtest.model;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.linktop.newlistviewtest.R;

import java.util.List;

/**
 * Created by linktoplinktop on 2017/6/20.
 */
public class FruitAdapter extends ArrayAdapter<Fruit> {

    private int resourceId;

    public FruitAdapter(Context context, int resource, List<Fruit> objects) {
        super(context, resource, objects);

        resourceId = resource;

    }

    //


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //convertView 复用的单元格
        //取出数据源
        Fruit fruitTemp = getItem(position);
        //xml生成view
        View itemView = convertView;
        if (itemView == null) {
            itemView = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
        }

        ImageView imageV = (ImageView)itemView.findViewById(R.id.fruit_headImage);
        TextView nameTv = (TextView)itemView.findViewById(R.id.fruit_name);

        nameTv.setText(fruitTemp.getFruitName());
        imageV.setImageResource(fruitTemp.getImageId());


        return itemView;
    }
}
